import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author dycong
 * @date 2018/11/30 9:08
 */
public class ImageWaterMark {

    /**
     * @param srcImgPath       源图片路径
     * @param tarImgPath       保存的图片路径
     * @param waterMarkContent 水印内容
     * @param markContentColor 水印颜色
     * @param font             水印字体
     */
    public void addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent, Color markContentColor, Font font) {

        try {
            // 读取原图片信息
            File srcImgFile = new File(srcImgPath);//得到文件
            Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
            int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
            int srcImgHeight = srcImg.getHeight(null);//获取图片的高
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.setColor(markContentColor); //根据图片的背景设置水印颜色
            g.setFont(font);              //设置字体

            //设置水印的坐标
//            int x = srcImgWidth - 2*getWatermarkLength(waterMarkContent, g);
//            int y = srcImgHeight - 2*getWatermarkLength(waterMarkContent, g);

            int x = srcImgWidth / 2;
            int y = srcImgHeight / 2;
            g.drawString(waterMarkContent, x, y);  //画出水印
            g.dispose();
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
            ImageIO.write(bufImg, "jpg", outImgStream);
            System.out.println("添加水印完成");
            outImgStream.flush();
            outImgStream.close();

        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }

    public static void main(String[] args) {
        Font font = new Font("微软雅黑", Font.PLAIN, 35);                     //水印字体
        String srcImgPath = "C:\\Users\\arlain\\Pictures\\Saved Pictures\\IMG_0840.JPG"; //源图片地址
        String tarImgPath = "C:\\Users\\arlain\\Desktop\\IMG_0748.JPG"; //待存储的地址
        String waterMarkContent = "图片来源：http://blog.csdn.net/zjq_1314520";  //水印内容
        Color color = new Color(0, 0, 0, 255);                               //水印图片色彩以及透明度
        new ImageWaterMark().addWaterMark(srcImgPath, tarImgPath, waterMarkContent, color, font);

    }

    @Test
    public void test001() throws IOException {
        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = e.getAvailableFontFamilyNames();
        Font[] fonts = e.getAllFonts();
        for (String fontName : fontNames) {
            getImage(fontName, Font.PLAIN);
        }

    }

    public static void getImage(String fontName, int fontStyle) throws FileNotFoundException, IOException {
        // 得到图片缓冲区
        int width = 100;
        int height = 50;
        int imageType = BufferedImage.TYPE_INT_BGR;
        BufferedImage myImage = new BufferedImage(width, height, imageType);

        // 得到画笔
        Graphics2D pen = (Graphics2D) myImage.getGraphics();

        // 设置笔的颜色,即背景色
        pen.setColor(Color.WHITE);

        // 画出一个矩形
        // 坐标x 坐标y 宽度100 长度50
        pen.fillRect(0, 0, 100, 50);

        // 微软雅黑 粗体显示 大小25
        Font font = new Font(fontName, fontStyle, 25);
        pen.setFont(font);

        // 字的颜色 和 背景的颜色 要不同的
        // 给最苦 第一次就没有注意到，结果 运行了三四次 都发现没有字呀
        pen.setColor(Color.blue);

        // 写字
        pen.drawString("A B C D", 20, 35);

        ImageIO.write(myImage, "JPEG", new FileOutputStream("C:\\Users\\arlain\\Desktop\\tets001\\" + fontName + ".jpg"));

    }

    @Test
    public void test() {

        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = e.getAvailableFontFamilyNames();
        System.out.println("---------------------------- size: " + fontNames.length);
        for (String fontName : fontNames) {
            System.out.println(fontName);
        }

        Font[] fonts = e.getAllFonts();
        System.out.println("---------------------------- size: " + fonts.length);
        for (Font font : fonts) {
            String fontName = font.getFontName();
            System.out.println(fontName);
        }

    }
}
